package logic

import (
	"context"
	"fmt"
	"shop-hub/models"

	"shop-hub/shoprpc/internal/svc"
	"shop-hub/shoprpc/shoprpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodAddLogic {
	return &GoodAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 商品添加
func (l *GoodAddLogic) GoodAdd(in *shoprpc.GoodAddRequest) (*shoprpc.GoodAddResponse, error) {
	// todo: add your logic here and delete this line

	var good models.Good
	models.DB.Debug().Where("title = ?", in.Title).Find(&good)
	if good.ID != 0 {
		return nil, fmt.Errorf("当前商品已存在")
	}

	good = models.Good{
		Title:  in.Title,
		Price:  float64(in.Price),
		Image:  in.Image,
		Num:    int(in.Num),
		UserId: int(in.UserId),
	}
	models.DB.Debug().Create(&good)

	return &shoprpc.GoodAddResponse{
		GoodId: int64(good.ID),
	}, nil
}
